package cheqex.dao;

import org.apache.torque.util.Criteria;
import java.util.ArrayList;
import java.util.List;

import java.sql.Connection;
/*
 AdminPeer:
 admin - initializes administrator account
 changeUsername - allows user to change username given the admin.id
 changePassword - allows user to change password given the admin.id 
 */
public class AdminPeer extends cheqex.dao.BaseAdminPeer
{
	
	public static boolean installAdmin(){
		Criteria criteria = new Criteria();
		criteria.add(AdminPeer.ID, 0);
		try{
			if(AdminPeer.doSelect(criteria).isEmpty())
				return true;
			else return false;
		}catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean addAdmin(){
		Criteria criteria = new Criteria();
		criteria.add(AdminPeer.ID, 0);
		criteria.add(AdminPeer.USERNAME, "admin");
		criteria.add(AdminPeer.PASSWORD, "admin");
		
		try{
			AdminPeer.doInsert(criteria);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean adminCheck(String username, String password){
		Criteria criteria = new Criteria();
		int id = 0;
		criteria.add(AdminPeer.ID, id);
		criteria.add(AdminPeer.USERNAME, username);
		criteria.add(AdminPeer.PASSWORD, password);
		
		try{
			if((boolean)AdminPeer.doSelect(criteria).isEmpty()!=true)
				return true;
			else{ 
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean adminUsername(String username){
		Criteria criteria = new Criteria();
		int id = 0;
		criteria.add(AdminPeer.ID, id);
		criteria.add(AdminPeer.USERNAME, username);
		
		try{
			if((boolean)AdminPeer.doSelect(criteria).isEmpty()!=true)				
				return true;
			else return false;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	/*	changeUsername
	 * 	--------------
	 * 	AUTHOR: Patrisha Louisse O Sales
	 * 	changeUsername updates the username in the database.	*/
	public static boolean changeUsername(String username){
		Criteria criteria = new Criteria();
		criteria.add(AdminPeer.USERNAME, username);
		
		Criteria scriteria = new Criteria();
		int id = 0;
		scriteria.add(AdminPeer.ID, id);
		
		try{
			AdminPeer.doUpdate(scriteria, criteria);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}		
	}
	/*		END OF changeUsername		*/
	
	/*	changePassword
	 * 	--------------
	 * 	AUTHOR: Patrisha Louisse O Sales
	 * 	changePassword updates the password in the database		*/
	public static boolean changePassword(String password){
		Criteria criteria = new Criteria();
		criteria.add(AdminPeer.PASSWORD, password);
		
		Criteria scriteria = new Criteria();
		int id = 0;
		scriteria.add(AdminPeer.ID, id);
		
		try{
			AdminPeer.doUpdate(scriteria, criteria);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}		
	}
	/*		END OF changePassword		*/
}